import { message } from "@/utils/message";
import ArticleApi, { ArticleQueryRequest } from "@/api/articles/article";
import { type PaginationProps } from "@pureadmin/table";
import { reactive, ref, onMounted, unref, nextTick } from "vue";
import { useRouter } from "vue-router";
import dayjs from "dayjs";
export function usePost() {
  const form: ArticleQueryRequest = reactive({
    onlyPublished: false,
    status: null,
    categoryId: 0,
    search: ""
  });
  const dataList = ref([]);
  const loading = ref(true);
  const pagination = ref<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    background: true
  });
  const columns: TableColumnList = [
    {
      type: "selection",
      width: 55,
      align: "left",
      hide: ({ checkList }) => !checkList.includes("勾选列")
    },
    {
      label: "序号",
      type: "index",
      width: 70,
      hide: ({ checkList }) => !checkList.includes("序号列")
    },
    {
      label: "标题",
      prop: "title",
      minWidth: 200
    },
    {
      label: "类别",
      prop: "type",
      minWidth: 150,
      cellRenderer({ row }) {
        if (row.type == 1) {
          return <el-tag>文章</el-tag>;
        } else if (row.type == 2) {
          return <el-tag type="success">公告</el-tag>;
        } else {
          return <el-tag type="info">未知</el-tag>;
        }
      }
    },
    {
      label: "状态",
      prop: "status",
      minWidth: 80,
      cellRenderer({ row }) {
        return (
          <x-status v-model={row.status} type="cell" enableText="已发布" />
        );
      }
    },
    {
      label: "发布时间",
      minWidth: 90,
      prop: "createTime",
      formatter: ({ createTime }) =>
        dayjs(createTime).format("YYYY-MM-DD HH:mm:ss")
    },
    {
      label: "操作",
      fixed: "right",
      width: 240,
      slot: "operation"
    }
  ];
  interface EditDrawer {
    show: boolean;
    editId: number;
  }
  const editDrawer = reactive<EditDrawer>({
    show: false,
    editId: 0
  });
  const commentDrawer = reactive<EditDrawer>({
    show: false,
    editId: 0
  });
  function handleUpdate(row) {
    (editDrawer.show = true), (editDrawer.editId = row?.id);
  }
  function handleComment(row) {
    (commentDrawer.show = true), (commentDrawer.editId = row?.id);
  }
  async function handleDelete(row) {
    if (row?.id) {
      // await VehicleApi.Remove(row.id);
      message("删除成功！", { type: "success" });
      onSearch();
    }
  }

  function handleSizeChange(val: number) {
    console.log(`${val} items per page`);
    nextTick(onSearch);
  }

  function handleCurrentChange(val: number) {
    console.log(`current page: ${val}`);
    nextTick(onSearch);
  }

  function handleSelectionChange(val) {
    console.log("handleSelectionChange", val);
  }
  const router = useRouter();
  const goEditPath = postId => {
    router.push({ path: "/article/article/edit", query: { id: postId } });
  };
  async function onSearch() {
    loading.value = true;
    const data = await ArticleApi.list(
      {
        page: unref(pagination).currentPage,
        limit: unref(pagination).pageSize
      },
      {
        ...form
      }
    );
    dataList.value = data.rows;
    unref(pagination).total = data.total;
    setTimeout(() => {
      loading.value = false;
    }, 500);
  }

  const resetForm = formEl => {
    if (!formEl) return;
    formEl.resetFields();
    onSearch();
  };

  onMounted(() => {
    onSearch();
  });

  return {
    form,
    loading,
    columns,
    dataList,
    pagination,
    editDrawer,
    commentDrawer,
    goEditPath,
    onSearch,
    resetForm,
    handleUpdate,
    handleComment,
    handleDelete,
    handleSizeChange,
    handleCurrentChange,
    handleSelectionChange
  };
}
